<?php
include_once $_SERVER['DOCUMENT_ROOT'] . "/library/PHPExcel/Classes/PHPExcel.php";
include_once $_SERVER['DOCUMENT_ROOT'] . "/library/phpqrcode/qrlib.php";
include_once $_SERVER['DOCUMENT_ROOT'] . "/conf/config.php";

$record_count = 0;
$target_file = $_SERVER['DOCUMENT_ROOT'] . "/assets/xls/" . $_FILES['userfile']['name'];

if (!empty($_FILES)) {
	if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $target_file))
		echo json_encode(array('result' => 'error'));
	else {
		// 엑셀 파일을 읽어 들인다.
		$oReader = PHPExcel_IOFactory::load($target_file);
		$oSheet = $oReader->getActiveSheet();
		
		// 엑셀 파일의 레코드 개수를 구한다.
		$highestRow = $oSheet->getHighestRow();
		
		// 쿼리 스트링을 만든다.
		// $generate_script = 'generate.php?records=' . $highestRow . '&target=' . $target_file;
		
		// 엑셀 파일을 닫는다.
		$oReader->disconnectWorksheets();
		unset($oSheet);
		unset($oReader);
		
		echo json_encode(array('result' => 'done', 'records' => $highestRow, 'file' => $target_file)) ;
	}
}
else echo json_encode(array('result' => 'error'));

/*
$fileElementName = 'userfile';
if(!empty($_FILES[$fileElementName]['error']))
{
	switch($_FILES[$fileElementName]['error'])
	{
		case '1':
			$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
			break;
		case '2':
			$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
			break;
		case '3':
			$error = 'The uploaded file was only partially uploaded';
			break;
		case '4':
			$error = 'No file was uploaded.';
			break;
		case '6':
			$error = 'Missing a temporary folder';
			break;
		case '7':
			$error = 'Failed to write file to disk';
			break;
		case '8':
			$error = 'File upload stopped by extension';
			break;
		case '999':
		default:
			$error = 'No error code avaiable';
	}
}
elseif(empty($_FILES['userfile']['tmp_name']) || $_FILES['userfile']['tmp_name'] == 'none')
{
	$error = 'No file was uploaded..';
}
else 
{
	$msg .= $_FILES['userfile']['name'] . "( ";
	$msg .= number_format(@filesize($_FILES['userfile']['tmp_name'])) . "바이트) 의 내용을 데이터베이스에 저장하였습니다.";
	
	move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_dir . '/' . $_FILES['userfile']['name']);
	
	// 엑셀 파일을 읽어 들인다.
	$oReader = PHPExcel_IOFactory::load($_FILES['userfile']['tmp_name']);
	$oSheet = $oReader->getActiveSheet();
	
	$highestRow = $oSheet->getHighestRow();
	
	$highestColumn = $oSheet->getHighestColumn();
	$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
	
	$dbconn = @mysql_connect($dbhost, $dbuser, $dbpass);
	@mysql_select_db($dbname, $dbconn);
	
	// 다음 지도 API 쿼리 스트링
	$request = 'http://apis.daum.net/maps/addr2coord?apikey=' . $daum_api_key . '&output=json&q=';
	
	// 데이터베이스 삽입 쿼리 스트링
	$excelQRY = "insert into qr_data values (";
	
	for ($row = 2; $row <= $highestRow; ++$row)
	{
		$oCell = array();
		for ($col = 0; $col < $highestColumnIndex; ++$col)
			$oCell[] = $oSheet->getCellByColumnAndRow($col, $row)->getValue();
		
		// 요청을 수행합니다.
		$map_request = $request . urlencode($oCell[10] . " " . $oCell[2] . " " . $oCell[3] . " " . $oCell[4]);
		$map_result = json_decode(file_get_contents($map_request), true);
		
		// 배열의 값을 변경한다.
		$oCell[13] = $map_result[channel][item][0][lat];
		$oCell[14] = $map_result[channel][item][0][lng];
		$oCell[11] = $oCell[5] . "." . $oCell[6] . "." . $oCell[7] . "." . $oCell[8] . ".png";
		$oCell[15] = $oCell[5] . "." . $oCell[6] . "." . $oCell[7] . "." . $oCell[8] . ".png";

		// QR Code 생성
		QRcode::png($code_key.$oCell[0], $code_loc.$oCell[11], "L", 4, 2);
		
		$tmpQRY = "";	
		foreach ($oCell as $obj)
			$tmpQRY .= "'" . $obj . "',";
		
		$execQRY = $excelQRY . substr($tmpQRY, 0, strlen($tmpQRY)-1) . ");";
		
		@mysql_query($execQRY, $dbconn);
	}
	
	
	//for security reason, we force to remove all uploaded file
	//@unlink($_FILES['userfile']);		
}

echo json_encode(array('error' => $error, 'msg' => $msg, 'rec' => $highestRow));
echo "{";
echo				"error: '" . $error . "',\n";
echo				"msg: '" . $msg . "'\n";
echo "}";

*/
?>